﻿/*------------------------------------------------------------------
Copyright (c) 2008 visualSingularity.net. All rights reserved.

This file is part of Hyperbolic control.

Hyperbolic control is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

Hyperbolic control is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------*/

using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Windows;
using Hyperbolic.Vectors;
using System.Windows.Controls;

namespace Hyperbolic.Model
{
    public class TreeModel : INotifyPropertyChanged, ITreeModel
    {
        #region fields
        private ObservableCollection<NodeModel> _nodeModelCollection = new ObservableCollection<NodeModel>();
        private NodeModel _rootNodeModel;
        private double _length = 0.2;  // distance between node and children
        EuclidianVector _velocity = new EuclidianVector();
        private ScreenVector _containerOrigin = new ScreenVector();
        private ScreenVector _containerBounds = new ScreenVector();
       
        #endregion

        #region properties


        /// <summary> Gets the root of the tree model.
        /// </summary>
        public NodeModel RootNodeModel
        {
            get
            {
                return _rootNodeModel;
            }
        }

        /// <summary> Gets the distance between a node and its children in the hyperbolic space.
        /// </summary>
        public double Length
        {
            get
            {
                return _length;
            }
        }


        public ScreenVector ContainerBounds
        {
            get { return _containerBounds; }
            set { _containerBounds = value; }
        }

        public ScreenVector ContainerOrigin
        {
            get { return _containerOrigin; }
            set { _containerOrigin = value; }
        }

        public ObservableCollection<NodeModel> NodeModelCollection
        {
            get { return _nodeModelCollection; }
        }

        #endregion

        #region ctor

        /// <summary> Constructor.
        /// </summary>
        /// <param name="root">The root of the real tree.</param>
        public TreeModel(IBindingNode root)
        {
            if (root.IsLeaf)
            {
                _rootNodeModel = new NodeModel(root, this);
            }
            else
            {
                _rootNodeModel = new CompositeNodeModel(root, this);
            }

            this.AddNode(_rootNodeModel);
            _rootNodeModel.LayoutHyperbolicTree();
        }


        #endregion

        #region methods

        public void AddNode(NodeModel nodeModel)
        {
            _nodeModelCollection.Add(nodeModel);
            nodeModel.PropertyChanged += new PropertyChangedEventHandler(Node_PropertyChanged);
        }

        /// <summary>
        /// For optimization purpose. Load only sub-collections when needed.
        /// </summary>
        /// <param name="parent">A parent <see cref="INodeModel"/>.</param>
        private void PopulateSubNodes(CompositeNodeModel parent)
        {
            int _parentIndex = _nodeModelCollection.IndexOf(parent);
            if (_parentIndex == _nodeModelCollection.Count - 1 || _nodeModelCollection[_parentIndex + 1].Node.Level <= parent.Node.Level)
            {
                for (int i = 0; i < parent.Children.Count; i++)
                {
                    NodeModel __node = parent.Children[i];
                    __node.PropertyChanged += new PropertyChangedEventHandler(Node_PropertyChanged);
                    _nodeModelCollection.Insert(_parentIndex + i + 1, __node);
                }
            }
        }

        /// <summary>
        /// For optimization purpose. remove sub-collections when not needed anymore.
        /// </summary>
        /// <param name="parent">A parent <see cref="INodeModel"/>.</param>
        private void ClearSubNodes(CompositeNodeModel parent)
        {
            int __indexToRemove = _nodeModelCollection.IndexOf(parent) + 1;
            while ((__indexToRemove < _nodeModelCollection.Count) && (_nodeModelCollection[__indexToRemove].Node.Level > parent.Node.Level))
            {
                _nodeModelCollection[__indexToRemove].PropertyChanged -= new PropertyChangedEventHandler(Node_PropertyChanged);
                _nodeModelCollection[__indexToRemove].IsExpanded = false;
                _nodeModelCollection.RemoveAt(__indexToRemove);
            }
        }

        private void Node_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsExpanded")
            {
                NodeModel __node = sender as NodeModel;
                if (__node != null && !__node.IsLeaf)
                {
                    if (__node.IsExpanded)
                    {
                        this.PopulateSubNodes(__node as CompositeNodeModel);
                    }
                    else
                    {
                        this.ClearSubNodes(__node as CompositeNodeModel);
                    }
                }
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

    }
}
